package com.hengpeng.itfinoss.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang.math.NumberUtils;
import org.springframework.ui.ModelMap;

import com.hengpeng.common.acl.vo.Passport;
import com.hengpeng.common.configuration.ConfigProvider;
import com.hengpeng.common.configuration.ConfigProviderFactory;
import com.hengpeng.common.constant.Constant;
import com.hengpeng.common.exception.ServiceResult;
import com.hengpeng.common.util.StringHelper;
import com.hengpeng.common.web.annotation.HtmlInputPattern;
import com.hengpeng.common.web.tags.TagConstant;
import com.hengpeng.itfinoss.util.RequestUtil;
import com.hengpeng.itfinoss.util.SignatureHelper;
import com.hengpeng.itfinoss.util.Validate;

public class BaseController
{
	/* 后台系统成功处理页面 */
	protected final static String SUCCESS = "success_info";

	/* 后台系统错误处理页面 */
	protected final static String ERROR = "error_info";

	private final static String SYSTEM_VERSION_PROPERTIES_NAME = "system-version";

	private final static String SYSTEM_VERSION_PROPERTIES_FILEPATH = "system-version.properties";

	public static String SYSTEM_VERSION = BaseController.getSystemVersion();

	@SuppressWarnings("rawtypes")
	public static String getSystemVersion()
	{
		ConfigProvider cp = null;
		try
		{
			cp = ConfigProviderFactory.getInstance(BaseController.SYSTEM_VERSION_PROPERTIES_FILEPATH);

		}
		catch (Exception e)
		{
		}
		if (cp == null)
			return "暂无版本号！";
		return (String) cp.getConfigByPrimaryKey(BaseController.SYSTEM_VERSION_PROPERTIES_NAME);
	}

	protected final <T> String returnSuccess(ModelMap map, String returnUrl)
	{
		map.put(Constant.MESSAGE, Constant.TRANSACTION_RESPONSE_MESSAGE_SUCCESS);
		if (returnUrl != null && returnUrl.length() > 0)
		{
			map.put(Constant.URL_KEY, returnUrl);
		}
		return SUCCESS;
	}

	/**
	 * <pre>
	 * 返回成功信息
	 * </pre>
	 * @param <T>
	 * @param map
	 * @param result
	 * @param returnUrl 提示信息出现后返回的连接
	 * @return
	 */
	protected final <T> String returnSuccess(ModelMap map, ServiceResult<T> result, String returnUrl)
	{
		map.put(Constant.MESSAGE, result.getMessage());
		if (returnUrl != null && returnUrl.length() > 0)
		{
			map.put(Constant.URL_KEY, returnUrl);
		}
		return SUCCESS;
	}

	/**
	 * <pre>
	 * 
	 * </pre>
	 * @param <T>
	 * @param map
	 * @param message
	 * @param returnUrl 提示信息出现后返回的连接
	 * @return
	 */
	protected final <T> String returnSuccess(ModelMap map, String message, String returnUrl)
	{
		map.put(Constant.MESSAGE, message);
		if (returnUrl != null && returnUrl.length() > 0)
		{
			map.put(Constant.URL_KEY, returnUrl);
		}
		return SUCCESS;
	}

	protected final <T> String returnSuccess(HttpServletRequest request, ModelMap map, String message, String returnUrl)
	{
		Map<String, Object> params = RequestUtil.fileParmas(request);
		map.put(Constant.PARAMS_KEY, params);
		map.put(Constant.MESSAGE, message);
		if (returnUrl != null && returnUrl.length() > 0)
		{
			map.put(Constant.URL_KEY, returnUrl);
		}
		return SUCCESS;
	}

	protected final <T> String returnSuccess(HttpServletRequest request, ModelMap map, ServiceResult<T> result,
			String returnUrl)
	{
		Map<String, Object> params = RequestUtil.fileParmas(request);
		map.put(Constant.PARAMS_KEY, params);
		map.put(Constant.MESSAGE, result.getMessage());
		if (returnUrl != null && returnUrl.length() > 0)
		{
			map.put(Constant.URL_KEY, returnUrl);
		}
		return SUCCESS;
	}

	/**
	 * <pre>
	 * 返回异常信息
	 * </pre>
	 * @param map
	 * @param ex
	 * @return
	 */
	protected final String returnError(ModelMap map, Throwable ex)
	{
		map.put(Constant.MESSAGE, ex.getMessage());
		return ERROR;
	}

	/**
	 * <pre>
	 * 返回错误信息
	 * </pre>
	 * @param map
	 * @param errorMessage
	 * @return
	 */
	protected final String returnError(ModelMap map, String errorMessage)
	{
		map.put(Constant.MESSAGE, errorMessage);
		return ERROR;
	}

	protected final String returnError(ModelMap map, String errorCode, String errorMessage)
	{
		map.put(Constant.MESSAGE, errorMessage);
		map.put(Constant.CODE_KEY, errorCode);
		return ERROR;
	}

	/**
	 * <pre>
	 * 得到用户回话信息
	 * </pre>
	 * @param session
	 * @return
	 */
	protected final Passport getUserSession(HttpSession session)
	{
		Passport passport = (Passport) session.getAttribute(Constant.PROFILE_SESSION_NAME);
		return passport;
	}

	/**
	 * <pre>
	 * 得到用户登陆编号
	 * </pre>
	 * @param session
	 * @return
	 */
	protected final String getUserName(HttpSession session)
	{
		return getUserSession(session).getUserId();
	}

	protected String trim(String text)
	{
		if (text == null)
			return null;
		else
			return text.trim();
	}

	protected boolean validateArrayNullOrEmpty(Object[]... array)
	{
		return Validate.validateArrayNullOrEmpty(array);
	}

	protected boolean validateObjectsNullOrEmpty(Object... obj)
	{
		return Validate.validateObjectsNullOrEmpty(obj);
	}

	protected boolean ValidatePattern(String str, HtmlInputPattern pattern)
	{
		if (str == null)
			return false;
		if (!str.matches(pattern.getPattern()))
		{
			return true;
		}
		return false;
	}

	public boolean ValidatePattern(String str, String pattern)
	{
		if (str == null)
			return false;
		if (!str.matches(pattern))
		{
			return true;
		}
		return false;
	}

	protected boolean ValidatePattern(String[] str, HtmlInputPattern[] pattern)
	{
		for (int i = 0; i < str.length; i++)
		{
			if (ValidatePattern(str[i], pattern[i]))
			{
				return true;
			}
		}
		return false;
	}

	protected boolean ValidatePattern(String[] str, String[] pattern)
	{
		for (int i = 0; i < str.length; i++)
		{
			if (ValidatePattern(str[i], pattern[i]))
			{
				return true;
			}
		}
		return false;
	}

	public <T> T filter(T obj)
	{
		if (obj != null)
		{
			if (obj.toString().equals("") || obj.toString().equals("-1"))
			{
				return null;
			}
		}
		return obj;
	}

	public static void main(String[] args)
	{
		Map<String, String> map = new HashMap<String, String>();
		map.put("123456", "e10adc3949ba59abbe56e057f20f883e");
		map.put("abc123", "e99a18c428cb38d5f260853678922e03");
		map.put("AaBbCc123456", "b4ed1471304ef746c5d01605814bfd29");
		map.put("1A2b3c4d5e6", "163d173bb27f568a7e822d01d9284e79");
		map.put("中文", "bcce109775e8e1972e9f5fcda3e12895");
		map.put("!@#$%", "507250b947cc397023a9595001fcf167");

		for (String key : map.keySet())
		{
			String value = map.get(key);
			System.out.println(key + "=" + value);
			System.out.println(key + "=" + SignatureHelper.generateMD5Signture(key, "utf-8"));
			System.out.println(map.get(key).equals(SignatureHelper.generateMD5Signture(key, "utf-8")) ? "相同" : "不同");
		}

		BaseController obj = new BaseController();

		if (obj.ValidatePattern(new String[] { "23", "char@126.com" }, new HtmlInputPattern[] { HtmlInputPattern.INT,
				HtmlInputPattern.EMAIL }))
		{
			System.out.println("没有通过验证");
		}

	}

	/**
	 * 后台默认分页，每页31条
	 * @param request
	 * @return
	 */
	public int getPageSize(HttpServletRequest request)
	{
		String listName = request.getParameter(TagConstant.REQUEST_PARAMETERNAME_PAGINATION_CURRENTPAGEINDEX);
		String pageSize = request.getParameter(TagConstant.REQUEST_PARAMETERNAME_PAGINATION_PAGESIZE + "4" + listName);
		if (!StringHelper.isEmpty(pageSize))
		{
			int int1 = NumberUtils.toInt(pageSize);
			if (int1 > 0)
			{
				return int1;
			}
		}
		return 31;
	}
}
